package com.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

import java.io.*;
import java.sql.SQLException;

@WebServlet(name = "HelloServlet", value = "/HelloServlet")
public class HelloServlet extends HttpServlet {



    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        String userId = request.getParameter("Eamil").toString();
        String pwd = request.getParameter("Password").toString();
        System.out.println(userId);
        System.out.println(pwd);
//        PrintWriter out = response.getWriter();
//        out.println(request.getParameter("first_name"));
//        String title = "Using GET Method to Read Form Data";
//        String docType =
//                "<!doctype html public \"-//w3c//dtd html 4.0 " +
//                        "transitional//en\">\n";
//        out.println(docType +
//                "<html>\n" +
//                "<head><title>" + title + "</title></head>\n" +
//                "<body bgcolor=\"#f0f0f0\">\n" +
//                "<h1 align=\"center\">" + title + "</h1>\n" +
//                "<ul>\n" +
//                "  <li><b>名字</b>："
//                + request.getParameter("first_name") + "\n" +
//                "  <li><b>姓氏</b>："
//                + request.getParameter("last_name") + "\n" +
//                "</ul>\n" +
//                "</body></html>");
        Login log = new Login();
        test t = new test();
        try {
           int num =  log.login(userId,pwd);
           if(num == 0){
               request.setAttribute("Msg", "用户名错误");
               request.getRequestDispatcher("index.jsp").forward(request, response);
           }else if(num == 10){
               request.setAttribute("Msg", "密码错误");
               request.getRequestDispatcher("index.jsp").forward(request, response);
           } else if (num == 1) {
               int id = t.LogId();
               System.out.println("用户的ID是："+id);
                request.setAttribute("userId",id);
               request.getRequestDispatcher("JSP/Product.jsp").forward(request, response);
           }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
